<template>
	<view class="main">
		<u-form labelPosition="left" :model="model" ref="uForm" labelWidth='90'>
			<u-form-item label="线缆类型 :" @click="show=true">
				<u--input v-model="model.cableClass" disabled disabledColor="#ffffff" placeholder="请选择线缆类型"
					border="none"></u--input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>
			<u-form-item label="规格型号 :" @click="show2=true">
				<u--input v-model="model.cableModel" disabled disabledColor="#ffffff" placeholder="请选择规格型号"
					border="none"></u--input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>

			<u-form-item label="测量长度 :">
				<view class="" style="border: 1px solid #bcbcbc;border-radius: 16rpx;width: 100%;">
					<u-input v-model="model.measuringLength" placeholder="请输入测量长度">
					</u-input>
				</view>
			</u-form-item>
			<u-form-item label="估算长度 :">
				<view class="" style="border: 1px solid #bcbcbc;border-radius: 16rpx;width: 100%;">
					<u-input v-model="model.estimatedLength" placeholder="请输入估算长度"></u-input>
				</view>
			</u-form-item>


			<u-form-item label="重量 :">
				<view class="" style="border: 1px solid #bcbcbc;border-radius: 16rpx;width: 100%;">
					<u-input v-model="model.weight" placeholder="请输入重量"></u-input>
				</view>
			</u-form-item>
		</u-form>
		<view class="" style="margin: 30rpx 0rpx;">
			<u-button type="success" text="确定" @click="add"></u-button>
		</view>
		<view class="">
			<u-button text="取消" @click="back"></u-button>
		</view>
		<u-picker :show="show" :columns="data1" @cancel="show=false" close="show=false" @confirm="selectXl"></u-picker>
		<u-picker :show="show2" :columns="data2" @cancel="show2=false" close="show2=false"
			@confirm="selectGg"></u-picker>
	</view>
</template>

<script>
	import {
		getClassPullDown,
		getModelByclass,
		addWeighingRecord,
		editWeightDetail,
		getWeightDetail
	} from "@/common/api.js"
	export default {
		data() {
			return {
				model: {
					cableClass: "",
					cableModel: "",
					estimatedLength: "",
					weight: "",
					measuringLength: "",
				},
				show: false,
				show2: false,
				data1: [],
				data2: []

			}
		},
		watch: {
			"model.cableClass": function(newVal) {

				if (!newVal) return;
				this.getModelByclass(newVal);
			},
		},
		methods: {
			getClassPullDown() {
				getClassPullDown().then((res) => {
					if (res.success) {
						this.data1 = [res.data];
					}
				})
			},
			getModelByclass(newVal) {
				getModelByclass({
					cableClass: newVal
				}).then((res) => {
					if (res.success) {
						this.data2 = [res.data];
					}
				});
			},
			selectXl(e) {
				if (this.model.cableClass != e.value[0]) {
					this.model.cableModel = ""
				}
				this.model.cableClass = e.value[0]
				this.show = false

			},
			selectGg(e) {
				this.model.cableModel = e.value[0]
				this.show2 = false
			},
			back() {
				uni.navigateBack();
			},
			add() {
				if (this.id) {
					editWeightDetail({
						...this.model,
						id: this.id
					}).then((res) => {
						if (res.success) {
							uni.$u.toast('修改成功')
							uni.navigateBack();
						}
					})
				} else {
					addWeighingRecord({
						...this.model
					}).then((res) => {
						if (res.success) {
							uni.$u.toast('添加成功')
							uni.navigateBack();
						}
					})
				}
			},
			getDetail(id) {
				getWeightDetail({
					id
				}).then((res) => {
					if (res.success) {
						for (let key in this.model) {
							this.model[key] = res.data[key];

						}
					}
				});
			}



		},
		onLoad(e) {
			if (e.id) { //编辑
				this.id = e.id
				this.getDetail(e.id)
			}
		},
		created() {
			this.getClassPullDown()
		}

	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 20rpx 30rpx;
	}
</style>